package com.authine.cloudpivot.controller;

import com.authine.cloudpivot.engine.api.model.organization.DepartmentModel;
import com.authine.cloudpivot.engine.api.model.organization.UserModel;
import com.authine.cloudpivot.enums.ResultCodeEnum;
import com.authine.cloudpivot.enums.TableNameEnum;
import com.authine.cloudpivot.ext.service.HouseSqlService;
import com.authine.cloudpivot.form.HouseSqlForm;
import com.authine.cloudpivot.foundation.orm.api.dml.JPANativeQuery;
import com.authine.cloudpivot.util.Result;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "应用模块::列表::定制房屋信息数据")
@RestController
@RequestMapping("/api/houseSql")
@Slf4j
public class QueryHouseSqlController extends BaseController {
    @Autowired
    private HouseSqlService houseSqlService;

    @Autowired
    private QuerySqlController querySqlController;

    @ApiOperation(value = "获取**街道对应表")
    @GetMapping("/getTableNameEnums")
    public Result getTableNameEnums() {
        return Result.ok(TableNameEnum.convertToMap());
    }

    @ApiOperation(value = "社区名称下拉框")
    @GetMapping("/getCommunity")
    public Result getCommunity(@RequestParam("tableCode") String tableCode, @RequestParam("communityIds") List<String> communityIds) {
        return Result.ok(houseSqlService.getCommunity(tableCode, communityIds));
    }

    @ApiOperation(value = "所属网格下拉框")
    @GetMapping("/getGrid")
    public Result getGrid(@RequestParam("tableCode") String tableCode, @RequestParam("communityId") String communityId) {
        return Result.ok(houseSqlService.getGrid(tableCode, communityId));

    }

    @ApiOperation(value = "tab页")
    @GetMapping("/getTab")
    public Result getTab(@RequestParam("tableCode") String tableCode, @RequestParam("communityId") String communityId, @RequestParam("gridId") String gridId) {
        if (StringUtils.isBlank(tableCode)) {
            tableCode = "DFL";
        }
        String queryCode = getQueryCode();
        return Result.ok(houseSqlService.getTab(tableCode, communityId, gridId, queryCode));

    }

    @Nullable
    private String getQueryCode() {
        String userId = getUserId();
        UserModel userModel = getUserFacade().get(userId);
        String departmentId = userModel.getDepartmentId();
        DepartmentModel department = getOrganizationFacade().getDepartment(departmentId, Boolean.FALSE);
        List<Map<String, Object>> deptById = querySqlController.getDeptById(departmentId);
        String queryCode = null;
        if (CollectionUtils.isNotEmpty(deptById)) {
            queryCode = department.getQueryCode();
        }
        return queryCode;
    }

    @ApiOperation(value = "列表")
    @PostMapping("/getListPage")
    public Result getListPage(@RequestBody HouseSqlForm form) {
        log.info("QueryHouseSqlController:getListPage:form:{}", form);
        if (StringUtils.isBlank(form.getTableCode())) {
            form.setTableCode("DFL");
        }
        String queryCode = getQueryCode();
        form.setQueryCode(queryCode);
        List<Map<String, Object>> list = houseSqlService.getList(form);
        Integer count = houseSqlService.getListCount(form);
        return Result.ok(list, count, ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage());
    }

}
